package com.baway.biliclitent.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baway.biliclitent.MainActivity;
import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.HomePagerAdapter;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;

/**
 * 关注 Fragment
 */

public class FragmentMain extends Fragment {
    private SlidingTabLayout sliding_tabs;
    private ViewPager mViewPager;
    private String[] title = {"直播","推荐","番剧","分区","关注","发现"};
    private FragmentManager manager;
    private ArrayList<Fragment> listFragment = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main,container,false);
        //获取Tablayout
        sliding_tabs = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);

        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        ((MainActivity) getActivity()).setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(R.drawable.ic_navigation_drawer);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //显示toolbar
        //获取AcationBar实例
        ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
        if(actionBar != null){
            //让导航按钮显示出来
            //Toolbar最左侧的按钮叫做HomeAsUp
            actionBar.setDisplayHomeAsUpEnabled(false);
            //点击图片进行打开
//            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        manager = getActivity().getSupportFragmentManager();
        setFragment();
    }

    /**
     *   添加 Fragment
     */
    private void setFragment(){
        FragmentLive fragment_live = new FragmentLive();
        FragmentRecommend fragment_recommend = new FragmentRecommend();
        FragmentTomato fragment_tomato = new FragmentTomato();
        FragmentZone fragment_zone = new FragmentZone();
        FragmentAttention fragment_attention = new FragmentAttention();
        FragmentDiscover fragment_discover = new FragmentDiscover();

        listFragment.add(fragment_live);
        listFragment.add(fragment_recommend);
        listFragment.add(fragment_tomato);
        listFragment.add(fragment_zone);
        listFragment.add(fragment_attention);
        listFragment.add(fragment_discover);
        initViewPager();
    }
    private void initViewPager() {
        //适配
        HomePagerAdapter mHomeAdapter = new HomePagerAdapter(manager,listFragment,
                title);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setAdapter(mHomeAdapter);
        sliding_tabs.setViewPager(mViewPager);
        mViewPager.setCurrentItem(1);
    }

}
